package com.cyandpos.pad.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cyandpos.pad.R;
import com.cyandpos.pad.cateringformat.morefunction.presenter.OrderFoodInfoPresenterImpl;
import com.cyandpos.pad.cateringformat.morefunction.view.OrderFoodInfoView;
import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.cateringformat.morefunction.bean.FoodtableInfo;
import com.cyandpos.pad.cateringformat.morefunction.adapter.OrderFoodtableInfoAdapter;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.CyFood;
import com.cyandpos.pad.posdata.TableReserve;
import com.cyandpos.pad.utils.ToastUtils;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

//预定详情
public class OrderFoodInfoActivity extends AppCompatActivity implements OrderFoodInfoView {

    private TextView dialog_title;
    private OrderFoodtableInfoAdapter infoAdapter;
    private FoodtableInfo foodtableInfo;
    private ListView ordertablelist;
    private ArrayList<FoodtableInfo> infoArrayList;
    private ArrayList<TableReserve> tableReserveArrayList=new ArrayList<>();

    private AlertDialog mDialog;//所有dialog
    private OrderFoodInfoPresenterImpl presenter;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_food_info);
        dialog_title = findViewById(R.id.tv_dialog_title_name);
        dialog_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  finish();
            }
        });
        dialog = new ProgressDialog(OrderFoodInfoActivity.this);
        dialog.setCancelable(false);

        ordertablelist = findViewById(R.id.lis_act_ordertable);
        infoArrayList = new ArrayList<>();

        presenter = new OrderFoodInfoPresenterImpl(this);

        presenter.loadOrderFoodInf(Integer.parseInt(ErpCommonData.loadConfig.getShopId())); //调用 清空
        infoAdapter = new OrderFoodtableInfoAdapter(OrderFoodInfoActivity.this, tableReserveArrayList);
        ordertablelist.setAdapter(infoAdapter);
        ordertablelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

              //  infoAdapter.setSelectPos(position,true);
                cancel_order_Dialog(position);

            }
        });

    }

    private void cancel_order_Dialog(final int position) {

        View view = LayoutInflater.from(this).inflate(R.layout.dialog_cancel_orderfood_layout, null);

        Button tablecancelOK = (Button) view.findViewById(R.id.btn_tablecancel_ok);
        Button tablecancelNo = (Button) view.findViewById(R.id.btn_tablecancel_back);
        TextView textView = (TextView) view.findViewById(R.id.tv_dialog_title_name);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();

            }
        });
        tablecancelNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        tablecancelOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                Toast.makeText(OrderFoodInfoActivity.this,"取消"+tableReserveArrayList.get(position).getId()+"号桌",Toast.LENGTH_SHORT).show();

                 cancel_orderfood(tableReserveArrayList.get(position).getBookNo());

            }
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(OrderFoodInfoActivity.this);
        builder.setView(view).setCancelable(false).create();
        mDialog = builder.show();

    }
    private void cancel_orderfood(String bookno) {

          //取消预定 接口
        PosHttpManager.cancelReserveTableHttpEexcute(bookno, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                   Log.i("TAG-----取消预定",response.toString());
                BaseResponse<Object> baseResponse = BaseResponse.fromJson(response.toString(), Object.class);

                if(baseResponse.getCode().equals("0")){
                    ToastUtils.showMsg("预定取消成功");

                }else{
                    ToastUtils.showMsg("预定取消失败"+baseResponse.getMsg().toString());
                }

                presenter.loadOrderFoodInf(Integer.parseInt(ErpCommonData.loadConfig.getShopId())); //调用后刷新

            }

            @Override
            public void onFailure(String msg) {

                ToastUtils.show(msg);

            }
        });




    }

    @Override
    protected void onResume() {
        super.onResume();
        dialog_title.setText("预定详情");
    }

    @Override
    public void showProgress(String msg) {
        dialog.setMessage(msg);
        dialog.show();
    }

    @Override
    public void dismissProgress() {
        dialog.dismiss();

    }

    @Override
    public void OrderFoodInfoSuccess(List<TableReserve> tableReserves) {
        if(tableReserves!=null&&tableReserves.size()>0) {
            tableReserveArrayList.clear();
            tableReserveArrayList.addAll(tableReserves);
            infoAdapter.notifyDataSetChanged();
        }else{
            tableReserveArrayList=new ArrayList<>();
            infoAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void OrderFoodInfoFailure(String errorMsg) {
        ToastUtils.show(errorMsg);

    }
}
